package com.coollang.smartbra.activity;

import android.content.DialogInterface;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.coollang.smartbra.R;
import com.coollang.smartbra.base.BaseActivity;
import com.coollang.smartbra.base.MyApplication;
import com.coollang.smartbra.bleutil.BleUtil;
import com.coollang.smartbra.presenter.DeviceInfoActivityPresenter;
import com.coollang.smartbra.utils.ActivitySwitcher;
import com.coollang.smartbra.utils.FontsUtil;
import com.coollang.smartbra.utils.PreferencesUtils;
import com.coollang.smartbra.utils.ToastUtils;
import com.coollang.smartbra.utils.TranslucentSystemBarUtils;
import com.coollang.smartbra.utils.UIUtils;
import com.coollang.smartbra.views.OptionCircle;
import com.coollang.smartbra.views.WaveView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 我的设备页面
 */
public class DeviceInfoActivity extends BaseActivity {

    @BindView(R.id.status_bar)
    View statusBar;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.content)
    RelativeLayout content;
    @BindView(R.id.btn_update_device)
    Button mBtnUpdate;
    @BindView(R.id.cicleview)
    OptionCircle mOptionCircle;
    @BindView(R.id.waveview)
    WaveView mWaveView;
    @BindView(R.id.tv_battery_count)
    TextView mTVBatteryCount;
    @BindView(R.id.tv_ble_info)
    TextView mTvBleInfo;
    @BindView(R.id.tv_ble_version)
    TextView mTvBleVersion;
    @BindView(R.id.tv_battery_info)
    TextView mTvBatteryInfo;

    private boolean mIsConnected;
    private DeviceInfoActivityPresenter mPresenter;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_device_info);
        ButterKnife.bind(this);
        // 设置沉浸式状态栏
        TranslucentSystemBarUtils.showChenJinZhuangTaiLan(statusBar, this, content);

        // 判断蓝牙是否连接
        mIsConnected = MyApplication.getApplication().isConnected;
        initViews();
        initListener();
    }

    private void initViews() {
        tvTitle.setText("我的设备");
        tvTitle.setTextColor(ContextCompat.getColor(this, R.color.black_80));

        // 判断蓝牙是否连接
        if (mIsConnected) {
            mTVBatteryCount.setTextSize(18);
            mOptionCircle.setCircleVisible(false);
            setWaveView();
        } else {
            mOptionCircle.setCircleVisible(true);
            //mOptionCircle.startCountdown();
            mWaveView.setShowWave(false);
            mTVBatteryCount.setText("点击连接设备");
            mTVBatteryCount.setTextSize(15);
        }

        String versionCode = "固件版本: " + MyApplication.getApplication().version;
        String deviceName = "序列号: " + MyApplication.getApplication().deviceName;

        mTvBleInfo.setText(deviceName);
        mTvBleVersion.setText(versionCode);
    }

    private void setWaveView() {
        mWaveView.setShowWave(true);
        mWaveView.setAmplitudeRatio(0.03f);
        mWaveView.setWaveLengthRatio(0.8f);
        mWaveView.setBorder(0, Color.parseColor("#00000000"));
    }

    private void setBatteryCount(String text) {
        SpannableString msp = new SpannableString(text);
        msp.setSpan(new RelativeSizeSpan(2.0f), 0, text.length() - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        msp.setSpan(FontsUtil.getInstance().getMyNumTypefaceSpan(), 0, text.length() - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        mTVBatteryCount.setText(msp);
    }

    private void initListener() {
    }

    @Override
    protected void initData() {
        mPresenter = new DeviceInfoActivityPresenter(this);

        if (mIsConnected) {
            isShowTextView(true);
            int battery = MyApplication.getApplication().betery;
            setbatrey(battery);
            boolean hasNewVersion = PreferencesUtils.getBoolean(UIUtils.getContext(), "hasNewVersion");
            if (hasNewVersion && mIsConnected) {
                mBtnUpdate.setBackgroundResource(R.drawable.shape_btn_stroke);
                mBtnUpdate.setTextColor(Color.parseColor("#F79444"));
            }
        } else {
            // 隐藏
            isShowTextView(false);
        }
    }

    private void isShowTextView(boolean isShow) {
        mTvBatteryInfo.setVisibility(isShow ? View.VISIBLE : View.GONE);
    }

    @OnClick({R.id.btn_hange_device, R.id.btn_update_device, R.id.tv_battery_count, R.id.iv_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_hange_device:
                // 判断是否有绑定设备或者连接设备
                showFinishDialog();
                break;
            case R.id.btn_update_device:
                if (MyApplication.getApplication().isConnected) {
                    if (MyApplication.getApplication().betery < 50) {
                        //ToastUtils.showToast(getString(R.string.betery_enough));
                    } else {
                        //showSaveDialog();
                    }
                } else {
                    ToastUtils.showToast(getString(R.string.ble_disconnect1));
                }

                break;
            case R.id.tv_battery_count:
                if ("点击连接设备".equals(mTVBatteryCount.getText().toString())) {
                    unBindSucceed();
                }
                break;
            case R.id.iv_back:
                finish();
                break;
            default:
        }
    }

    private void showSaveDialog() {
        AlertDialog dialog = new AlertDialog.Builder(this).create();
        dialog.setMessage(getString(R.string.need_update));
        dialog.setButton(DialogInterface.BUTTON_NEGATIVE, getString(R.string.cancel), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // 点击事件的回调方法
            }
        });

        dialog.setButton(DialogInterface.BUTTON_POSITIVE, getString(R.string.ok), new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                ActivitySwitcher.goDeviceUpdateAct(DeviceInfoActivity.this);
                finish();
            }
        });
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();

        Button posiviveButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
        posiviveButton.setTextColor(Color.parseColor("#F79444"));
        Button negativeButton = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
        negativeButton.setTextColor(Color.parseColor("#000000"));
    }

    private void showFinishDialog() {
        AlertDialog dialog = new AlertDialog.Builder(this).create();
        dialog.setMessage(getString(R.string.delete_device));
        dialog.setButton(DialogInterface.BUTTON_NEGATIVE, getString(R.string.cancel), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // 点击事件的回调方法
            }
        });

        dialog.setButton(DialogInterface.BUTTON_POSITIVE, getString(R.string.ok), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (BleUtil.getInstance().isDecviceConnected()) {
                    BleUtil.getInstance().disconnect();
                }

                mPresenter.unBindDevice(MyApplication.getApplication().deviceMac);

            }
        });
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();

        Button posiviveButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
        posiviveButton.setTextColor(Color.parseColor("#F79444"));
        Button negativeButton = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
        negativeButton.setTextColor(Color.parseColor("#000000"));
    }

    public void setbatrey(int batrey) {
        int frontWaveColor;

        if (batrey > 20) {
            frontWaveColor = getResources().getColor(R.color.ble_electric_color_1);
        } else {
            frontWaveColor = getResources().getColor(R.color.ble_electric_color_2);
        }
        mWaveView.setWaveColor(getResources().getColor(R.color.tra), frontWaveColor);
        mWaveView.setWaterLevelRatio(batrey / 100.0f);
        setBatteryCount(batrey + "%");
    }

    public void unBindSucceed() {
        // 解除绑定成功
        BleUtil.getInstance().disconnect();
        ActivitySwitcher.goSearchAct(this);
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // mOptionCircle.stopAnimation();
    }
}
